from pms.common.views import PostFormula
from project.models import ProjectDocument
from project.utils.document import update_document, document_view
from project.utils.permission import assert_project_permissions


class API(PostFormula):
    def handle(self, req):
        doc_id = req['id']
        mod = req['mod']
        document = ProjectDocument.objects.get(id=doc_id)
        assert_project_permissions(document.pid_id, self.user.id, [2])
        return self.success_respond(document_view(
            document=update_document(
                document=document,
                view=mod
            )
        ))
